// ==UserScript==
// @name         msunhis login
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  msunhis login
// @author       changchengcheng
// @updateURL    https://gitee.com/chchcome/scripts/raw/master/msunLogin.js
// @downloadURL  https://gitee.com/chchcome/scripts/raw/master/msunLogin.js
// @match        *://*.msunhis.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=msunhis.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    console.log('msunhis login plugin')
    // Your code here...
    const orignalSetItem = sessionStorage.setItem;
    sessionStorage.setItem = function(key, newValue) {
        orignalSetItem.apply(this, arguments);
        if(key==='currentUser'){
           //console.log('session hack ',key,newValue);
           try{
             orignalSetItem.apply(this, ['immediatePass',1]);
             orignalSetItem.apply(this, ['ttPass',1]);
           }catch(e){
             console.error(e)
           }
        }
    };
    const xhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
      const xhr = this;
      if (arguments[1].includes('checkSmsMessage')) {
        const getter = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'response').get;
        const mockResult = {
            success: true,
            businessException: false,
            code: '0000'
        };
        Object.defineProperty(xhr, 'response', {
          get: () => {
            let result = getter.call(xhr);
            //console.log('xhr mock ',result)
            return JSON.stringify(mockResult);
          }
        });
        Object.defineProperty(xhr, 'responseText', {
          get: () => {
            let result = getter.call(xhr);
            //console.log('xhr mock ',result)
            return JSON.stringify(mockResult);
          }
        });
      }
      return xhrOpen.apply(xhr, arguments);
    };
})();